package com.learn.core.controller.manager;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LChapter;
import com.learn.core.dto.LChapterDTO;
import com.learn.core.service.ILChapterService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:41:29
 * @describe 章节表前端控制器
 */

@Api("章节表")
@RestController
@RequestMapping("/manager/chapter")
public class LChapterController extends WebController<LChapter> {
    @Autowired
    private ILChapterService iLChapterService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/tree")
    public CommonResult<Object> list(@RequestBody LChapterDTO lChapterDTO){
        try {
            QueryWrapper<LChapter> queryWrapper = new QueryWrapper<LChapter>();
            if(lChapterDTO.getCourseId()!=null)
                queryWrapper.eq("course_id",lChapterDTO.getCourseId());
            else {
                return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
            }
            queryWrapper.orderByAsc("chapter_seq");
            List<LChapter> lChapters = iLChapterService.queryLChapterAll(queryWrapper);
            if(lChapters.size()>0)
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,lChapters);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
    }


    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加章节", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LChapter lChapter, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lChapter!=null) {
                if (StringUtils.isEmpty(lChapter.getChapterName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_CHA_NAME);
                }
                if (StringUtils.isEmpty(lChapter.getChapterType())) {
                    lChapter.setChapterType("1");
                }
                if(lChapter.getChapterItem()==null){
                    lChapter.setChapterItem(0);
                }
                if (StringUtils.isEmpty(lChapter.getChapterSeq())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_CHA_SEQ);
                }
                if (StringUtils.isEmpty(lChapter.getCourseId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_CHA_COURSE);
                }
                if(StringUtils.isEmpty(lChapter.getLearnType())){
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_CHA_LEARN_TYPE);
                }


                lChapter.setCreateTime(new Date());
                lChapter.setCreateUser(userId);
                if (iLChapterService.save(lChapter)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lChapter.getChapterId());
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除章节", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LChapter lChapter){
        try {
            if(lChapter!=null){
                if(lChapter.getChapterId()!=null){
                    List<Integer> ids = new ArrayList<>();
                    ids.add(lChapter.getChapterId());
                    if(nodeStr(ids)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    private Boolean nodeStr (List<Integer> lists) throws Exception{
        List<Integer> ids = new ArrayList<>();
        QueryWrapper<LChapter> queryWrapper = new QueryWrapper<LChapter>();
        for (int i = 0; i <lists.size() ; i++) {
            int orgid = lists.get(i);

            queryWrapper.eq("chapter_seq",orgid);
            List<LChapter> lChapters = iLChapterService.list(queryWrapper);
            for (int j = 0; j < lChapters.size(); j++) {
                LChapter lChapter = lChapters.get(j);
                ids.add(lChapter.getChapterId());
            }
        }
        if(ids.size() > 0){
            this.nodeStr(ids);
        }

        ids.addAll(lists);
        queryWrapper = new QueryWrapper<LChapter>();
        queryWrapper.in(true, "chapter_id", ids);
        iLChapterService.remove(queryWrapper);

        return Boolean.TRUE;
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新章节", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LChapter lChapter, HttpServletRequest request) {
        if(lChapter!=null){
            // token
            String token = request.getHeader("token");
            String userId = JwtUtil.getUserId(token);
            lChapter.setUpdateTime(new Date());
            lChapter.setUpdateUser(userId);
            if(iLChapterService.updateById(lChapter)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}

